<?php
declare (strict_types=1);

namespace app;

use app\model\Users;
use think\App;
use think\exception\ValidateException;
use think\model;
use think\Validate;
use utils\Auth;

/**
 * 控制器基础类
 */
abstract class BaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * @var model 当前用户的对象
     */
    protected model $user;

    /**
     * @var string 当前用户的TOKEN
     */
    protected string $token;
    protected Auth $auth;
    private $str;

    /**
     * 构造方法
     * @access public
     * @param App $app 应用对象
     */
    public function __construct(App $app)
    {
        $this->app = $app;
        $this->request = $this->app->request;

        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {
        $this->str = '';
        if ($this->request->isLogin) {
            $this->user = $this->request->user;
            $this->token = $this->request->token;
        } else {
            $this->request->user = new Users();
            $this->user = $this->request->user;
            $this->token = '';
        }
        $this->auth = new Auth();
    }

    /**
     * 验证数据
     * @access protected
     * @param array $data 数据
     * @param string|array $validate 验证器名或者验证规则数组
     * @param array $message 提示信息
     * @param bool $batch 是否批量验证
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, string|array $validate, array $message = [], bool $batch = false)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                [$validate, $scene] = explode('.', $validate);
            }
            $class = str_contains($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        return $v->failException(true)->check($data);
    }


    protected function success(string $msg, null|array|object|string|int $data = null): \think\response\Json
    {
        return json([
            'code' => 1,
            'msg' => $msg,
            'data' => $data,
            'time' => time()
        ]);
    }

    protected function error(string $msg, null|array|object|string|int $data = null): \think\response\Json
    {
        return json([
            'code' => 0,
            'msg' => $msg,
            'data' => $data,
            'time' => time()
        ]);
    }

    protected function checkLogin($isAdmin = false): bool
    {
        if ($this->request->isLogin === false) return abort(json([
            'code' => 401,
            'msg' => '请先登录',
            'time' => time()
        ]));
        if ($isAdmin && $this->auth->check('admin', $this->user->id) === false) return abort(json([
            'code' => 403,
            'msg' => '权限不足',
            'time' => time()
        ]));
        return true;
    }

}
